<?php

// +----------------------------------------------------------------------
// | 新闻管理
// +----------------------------------------------------------------------
// | Author: Helly
// +----------------------------------------------------------------------
// | Date: 2014/10/22 11:12
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;
use Admins\Lib\Admin;

class NewsController extends AdminBase {

  
    //新闻分类
    public function classlist(){
        $model = M('NewsClass');
        $data = $model->select();
        $newsClass =array(); 
        foreach ($data as $key => $value) {
            if($value['fid'] == 0){
                $newsClass[$value['id']] = $value;
            }else{
                $newsClass[$value['fid']]['son'][] = $value;
            }
        }
        $this->assign("newsClass", $newsClass);
        $this->display();

    }
    
    //类别添加
    public function classadd(){
        $model=M('NewsClass');
        if(IS_POST){
            $data = I('post.');
            $act = I('get.action');
            if(empty($data['name'])){
                $this->error('请填栏目名称');
                exit;
            }
            
            if($act == 'add'){
                $data['create_time'] = time();
                if($model->add($data)){
                    $this->success('类别添加成功',U('classlist'));
                }else{
                    $this->error('类别添加失败');
                }
            }else if($act=='edit'){
                if($model->save($data))
                    $this->success('类别修改成功',U('classlist'));
                else
                    $this->error('类别修改失败或信息未更改');

            }
            
           
        }

        $id = I('get.id');
        $action = I('get.action');

        if(!empty($id)){
            $data = $model->where(array('id'=>$id))->find();

        }else{
            $data['id'] = 0;
            $data['name'] = '≡ 作为一级栏目 ≡';

        }
       
        $this->assign('actiones',$action);
        $this->assign("data", $data);
        $this->display();
    }

    
    //类别编辑
    public function classedit(){
        $model=M('NewsClass');
        $id = I('get.id');
        $action = I('get.action');

        $classData = $model->where(array('id'=>$id))->find();
        
        if($classData['fid'] == 0){
            $data['id'] = 0;
            $data['name'] = '≡ 作为一级栏目 ≡';
        }else{
            $data = $model->where(array('id'=>$classData['fid']))->find();

        }
        $this->assign('actiones',$action);
        $this->assign("data", $data);
        $this->assign("class", $classData);
        $this->display();
    }

    //类别排序更新
    public function catesort(){
        foreach ($_POST['sort'] as $id => $sort) {
            M('NewsCate')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }


    //删除类别
    public function catedelete(){
        $id = I('get.cateid');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if (D('Admins/News')->deletecate($id)) {
            $this->success("删除成功！");
        } else {
            $this->error(D('Admins/News')->getError()? : '删除失败！');
        }
    }

    //排序更新
    public function sort(){
        foreach ($_POST['sort'] as $id => $sort) {
            M('News')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }

    //新闻列表
    public function index() {
        if (IS_POST) {
            $this->redirect('index', $_POST);
        }

        //搜索关键字
        $keyword = \Input::getVar(I('get.keyword'));
        if (!empty($keyword)) {
            $this->assign("keyword", $keyword);
            $where['title'] = array("LIKE", "%{$keyword}%");
        }

        //分类搜索
        $cateid1 = I('get.cateid1');
        $cateid2 = I('get.cateid2');
        if(!empty($cateid1)){
           
            //获取二级分类
            $newsTwo = M('NewsClass')->where(array('fid'=>$cateid1,'style'=>1))->select();
            $this->assign('newsTwo',$newsTwo);
            $this->assign('cateid2',$cateid2);
             $this->assign('cateid1',$cateid1);
            $where['cateid1'] = $cateid1;
        }
        
        if(!empty($cateid2)){
            
            $where['cateid2'] = $cateid2;
        }

        $count = M('News')->where($where)->count();
        $page = $this->page($count, 20);
        $News = M('News')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();
       
        //获取新闻分类
        $newsClass = M('NewsClass')->where(array('fid'=>0))->select();
        foreach($newsClass as $v){
            $cname[$v['id']] = $v['name'];
            if($v['style'] == 1){
                $classlist[] = $v;
            }
        }

        $this->assign("newslist", $News);
        $this->assign("Page", $page->show());
        $this->assign('cname',$cname);
        $this->assign('classlist',$classlist);
        $this->display();
    }


    //添加新闻信息
    public function add() {
       if (IS_POST) {
            $post=$_POST;
            if(empty($post['content'])){
                $this->error('请填写内容');
            }
            $post['create_time']=time();
            if (D('Admins/News')->addModel($post)) {
                    $this->success("信息添加成功！", U('News/index'));
            } else {
                $error = D('Admins/News')->getError();
                $this->error($error ? $error : '信息添加失败！');            }
        }

        //获取新闻分类
        $newsClass = M('NewsClass')->where(array('fid'=>0,'style'=>1))->select();  

        $this->assign("classlist", $newsClass);
        $this->display();
    }

    //编辑信息
    public function edit(){
        if(IS_POST){

            if (false !== D('Admins/News')->editModel($_POST)) {
                //$this->success("信息更新成功！",U($goback));
                
                $this->success("信息更新成功！",U('News/index'));
            } else {
                $error = D('Admins/News')->getError();
                $this->error($error ? $error : '信息修改失败！');
            }
        }else{
            
            $id=I('get.id',0,'intval');
            $where=array('id'=>$id);
            $info=M('News')->where($where)->find();
            
            //获取新闻分类
            $newsClass = M('NewsClass')->where(array('fid'=>0,'style'=>1))->select();
            $newsTwo = M('NewsClass')->where(array('fid'=>$info['cateid1'],'style'=>1))->select();
            $this->assign('info',$info);
            $this->assign("classlist", $newsClass);
            $this->assign('newsTwo',$newsTwo);
            $this->display();
        }
    }

    //单个删除
    public function delete() {
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if (D('Admins/News')->deleteBatch($id)) {
            $this->success("删除成功！");
        } else {
            $this->error(D('Admins/News')->getError()? : '删除失败！');
        }
    }

    //新闻广告
    public function advert(){
       $model=M('NewsAdvert');
       $act = I('get.action');
       if($act){
         $where['seat'] = $act;
       }

       $data = $model->where($where)->order('seat ASC,create_time desc')->select();
   
       $this->assign('act',$act);
       $this->assign("data", $data);
       $this->display();
    }

    //新闻广告新增与编辑
    public function advertadd(){
         $model=M('NewsAdvert');
        if(IS_POST){
            $data = I('post.');
            $act = I('get.action');
            if(empty($data['title'])){
                $this->error('广告标题不能为空');
                exit;
            }
            if(empty($data['image'])){
                $this->error('请上传广告图片');
                exit;
            }
           if($act == 'add'){
                $data['create_time'] = time();
                if($model->add($data)){
                    $this->success("新增广告成功！",U('advert'));
                }else{
                    $this->error('新增广告失败');
                }
            }else if($act=='edit'){

                if($model->where(array('id'=>$data['id']))->save($data))
                    $this->success('广告修改成功',U('advert'));
                else
                    $this->error('广告修改失败或信息未更改');

            }
        }

        $this->display();

    }

    //编辑广告
    public function advertedit(){

        $model = M('NewsAdvert');
        $id = I('get.id');
        $data = $model->where(array('id'=>$id))->find();

        $this->assign("data", $data);
        $this->display();
    }

    //删除新闻广告 
    public function advertdel(){
        $id = I('get.cateid');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if (M('NewsAdvert')->delete($id)) {
            $this->success("删除成功！");
        } else {
            $this->error('删除失败！');
        }

    }


    //批量处理信息(审核,取消审核,删除)
    public function batch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
                case 'checktj':
                    $data=array('iftj'=>true);
                    $msg='推荐';
                    break;
                case 'canceltj':
                    $data=array('iftj'=>false);
                    $msg='取消推荐';
                    break;
            }
            foreach ($ids as $id) {
                M('News')->where(array('id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }


    //批量删除信息及信息附件
    public function deleteAll(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if(D('Admins/News')->deleteBatch($id)){
                    continue;
                }else{
                    $error = D('Admins/News')->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }

    //新闻专题
    public function special(){
        $model=M('NewsSpecial');
       
        //搜索关键字
        $keyword = \Input::getVar(I('post.keyword'));
        if (!empty($keyword)) {
            $this->assign("keyword", $keyword);
            $where['title'] = array("LIKE", "%{$keyword}%");
        }

        $count = $model->where($where)->count();
        $page = $this->page($count, 10);
        $list = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();

        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }

    public function spladd(){
        $model=M('NewsSpecial');
        if(IS_POST){
            $data = I('post.');
            $act = I('get.action');
            if(empty($data['title'])){
                $this->error('专题标题不能为空');
                exit;
            }
            if(empty($data['image'])){
                $this->error('请上传广告图片');
                exit;
            }
           if($act == 'add'){
                $data['create_time'] = time();
                if($model->add($data)){
                    $this->success("新增专题成功！",U('special'));
                }else{
                    $this->error('新增专题失败');
                }
            }else if($act=='edit'){

                if($model->where(array('id'=>$data['id']))->save($data))
                    $this->success('专题修改成功',U('special'));
                else
                    $this->error('专题修改失败或信息未更改');

            }
        }
        $this->display();
    }

     public function spledit(){
        $id = I("get.id");
        $where['id'] = $id;
        $data = M('NewsSpecial')->where($where)->find();

        $this->assign('data',$data);
        $this->display();
     }

     public function spldelete(){
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if (M('NewsSpecial')->delete($id)) {
            $this->success("删除成功！");
        } else {
            $this->error('删除失败！');
        }
     }

    //ajax远程处理
    public function ajax(){
        $id =  I('get.id');
        $newsClass = M('NewsClass')->where(array('fid'=>$id,'style'=>1))->select(); 
        if($newsClass) {
             $htmlOption='';
            foreach ($newsClass as $key => $value) {
               $htmlOption.= "<option value='".$value['id']."'>".$value['name']."</option>";
            }
            echo  $htmlOption;
        }else{
            echo 'no';
        }

    }


}
